﻿using System;
using System.Linq;
using System.Web.UI;
using Coolite.Ext.Web;
using DayPilot.Web.Ui;
using Pap.Web.Modules.Administration.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Administration.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class YearVacations : BasePage<YearVacationsViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.IsAjaxRequest && !Page.IsPostBack)
            {
                calVavations.EventClickHandling = UserActionHandling.PostBack;
                calVavations.FreetimeClickHandling = UserActionHandling.PostBack;

                DateTime nowDate = DateTime.Today;
                mpMonths.YearStart = nowDate.Year;
                mpMonths.YearEnd = nowDate.Year;

                RefreshCalendar(nowDate);
            }
        }




        /// <summary>
        /// Handles the Clicked event of the btnView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnView_Clicked(object sender, EventArgs e)
        {
            RefreshCalendar(new DateTime(mpMonths.Year, mpMonths.Month, 1));
        }

        /// <summary>
        /// Handles the FreeTimeClicked event of the calVacations control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DayPilot.Web.Ui.FreeClickEventArgs"/> instance containing the event data.</param>
        protected void calVacations_FreeTimeClicked(object sender, FreeClickEventArgs e)
        {
            this.ViewModel.InsertVacDate(e.Start);
            RefreshCalendar(e.Start);
        }

        /// <summary>
        /// Handles the EventClicked event of the calVacations control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DayPilot.Web.Ui.EventClickEventArgs"/> instance containing the event data.</param>
        protected void calVacations_EventClicked(object sender, EventClickEventArgs e)
        {
            DateTime deletedDate = DateTime.Parse(e.Value);
            this.ViewModel.DeleteVacDate(deletedDate);
            RefreshCalendar(deletedDate);
        }

        /// <summary>
        /// Refreshes the calendar.
        /// </summary>
        /// <param name="requestedDate">The requested date.</param>
        private void RefreshCalendar(DateTime requestedDate)
        {
            calVavations.StartDate = requestedDate.AddDays(-(requestedDate.Day - 1));
            calVavations.Days = DateTime.DaysInMonth(requestedDate.Year, requestedDate.Month);

            var vacations = from date in this.ViewModel.GetVacationByMonth(requestedDate.Month)
                            select new
                            {
                                ID = date,
                                Start = date,
                                End = date.AddHours(10),
                                Name = "Vacation"
                            };

            calVavations.DataSource = vacations;
            calVavations.DataBind();
        }
    }
}
